package com.httpClient.chapter2;

import com.httpClient.util.ResponseUtil;
import com.sky.JULLog.MyJulLogger;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeaderElementIterator;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

/**
 * @author : wushikai
 *         <p>
 *         date : 2024-06-19
 */
public class ProxyClient {


    private static Logger log = MyJulLogger.getLogger(ProxyClient.class);


    public static void main(String[] args){

        CloseableHttpClient httpClient = HttpClients.createDefault();

        String baiduUrl = "https://www.baidu.com/";
        String gitHubUrl = "http://github.com/";
        HttpGet httpGet = new HttpGet(gitHubUrl);


        String  ip  = "112.19.241.37" ;
        int  port = 19999;


        HttpHost proxy= new HttpHost(ip , port );  //mark  开放代理由第三方服务器提供，IP不确定性较大，总体质量不高。如需购买基于自营服务器的高质量IP产品，欢迎提前试用。

        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
                .setConnectTimeout(5000)            // mark  设置 TCP 三次握手的超时时间
                .setSocketTimeout(3000)             // mark  设置从 请求网址连接之后 , 获取数据的时间 , 例如你请求db 没有索引的数据, db请求很久, 即使连接了, 也会返回超时
                .setConnectionRequestTimeout(5000)  // mark 从连接池获取 connection 的超时时间 , 当出现大量请求, 连接池拿不出的时候, 报超时
                .build();

        httpGet.setConfig( requestConfig);

        try {
            CloseableHttpResponse     response = httpClient.execute(httpGet);
            ResponseUtil.handleResponse(response);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }




}
